package com.udaos.modules.physical.utils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class CodeUtil {
	
	 /**
     * 生成数字串方法
     * 
     * @param quantity 生成数量
     * @param length 生成数字串长度
     * @return 字符串格式的数字串数组
     */
    public static String[] numericString(int quantity, int length) {
        // 声明结果集长度
        String[] res = new String[quantity];
        // 开始生成数字串
        for (int i = 0; i < quantity; i++) {
            StringBuffer sb = new StringBuffer();
            for (int j = 0; j < length; j++) {
                sb.append(new Random().nextInt(10));
            }
            // 生成一个数字串保存在结果集中
            res[i] = sb.toString();
            // 开始对比保存的数据
            for (int j = 0; j < i; j++) {
                // 取50%相似值
                int similarity = length / 2;
                char[] y = res[i].toCharArray();
                char[] t = res[j].toCharArray();
                for (int k = 0; k < y.length; k++) {
                    if (y[k] == t[k]) {
                        similarity--;
                    }
                }
                // 不符合相似值，下标缩进
                if (similarity <= 0) {
                    i--;
                    break;
                }
            }
        }
        return res;
    }

	/**
	 * 获取4为随机码
	 * @return
	 */
	public static String getCode(){
		String[] beforeShuffle = new String[] { "2", "3", "4", "5", "6", "7",  
                "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",  
                "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",  
                "W", "X", "Y", "Z" };  
        List list = Arrays.asList(beforeShuffle);  
        Collections.shuffle(list);  
        StringBuilder sb = new StringBuilder();  
        for (int i = 0; i < list.size(); i++) {  
            sb.append(list.get(i));  
        }  
        String afterShuffle = sb.toString();  
        String result = afterShuffle.substring(5, 9);  
        
        return result;
	}
}
